﻿/**
 * @author Colin
 * timeformat introduce from trendGraph
 */
class com.mixmedia.utils.TimeUtils {
	public static function timeCodeToDate(str:String):Date{
		var timeCode:String = str;//YYYYMMDDHHiiss
		var year = Number(timeCode.substr(0,4));
		var month = Number(timeCode.substr(4,2))-1;//start from 0-11
		var date = Number(timeCode.substr(6,2));
		var hour = Number(timeCode.substr(8,2));
		var min = Number(timeCode.substr(10,2));
		var sec = Number(timeCode.substr(12,2));
		return new Date(year,month,date,hour,min,sec);
	}

	public static function dateToTimeCode(d:Date,round:String):String{
		if(round==null)round="sec";

		//use for match scroller timestamp and datasource
		var year = d.getFullYear();
		if(round=='year')return String(year)+'0101000000';
		
		var month = d.getMonth()+1;
		if(month<10)month="0"+month;
		if(round=='month')return String(year)+String(month)+'01000000';

		var date = d.getDate();
		if(date<10)date="0"+date;

		if(round=='day')return String(year)+String(month)+String(date)+'000000'; 

		var hour = d.getHours();
		if(hour<10)hour="0"+hour;
		if(round=='hour')return String(year)+String(month)+String(date)+String(hour)+'0000';

		var min = d.getMinutes();
		if(min<10)min="0"+min;
		if(round=='min')return String(year)+String(month)+String(date)+String(hour)+String(min)+'00';

		var sec = d.getSeconds();
		if(sec<10)sec="0"+sec;
		if(round=='sec')return String(year)+String(month)+String(date)+String(hour)+String(min)+String(sec);
	}
}